package com.newlink.building.userinfo.adapter

import android.view.View
import android.widget.ImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.newlink.building.common_base.model.bean.AuthResponseBody
import com.newlink.building.common_base.utils.NLog
import com.newlink.building.userinfo.R

/**
 * @author Jake

 */
class Module_UserInfo_CardDataAdapter(data: MutableList<AuthResponseBody>) :
    BaseQuickAdapter<AuthResponseBody, BaseViewHolder>(R.layout.item_card_data, data),
    LoadMoreModule {

    private var mOnDeleteClickListener: OnDeleteClickListener? = null
    private var mOnItemClickListener: OnItemClickListener? = null

    fun setOnDeleteClickListener(listener: OnDeleteClickListener) {
        mOnDeleteClickListener = listener
    }

    fun setOnFvlItemClickListener(listener: OnItemClickListener) {
        mOnItemClickListener = listener
    }


    fun updateList(data1: MutableList<AuthResponseBody>) {
        this.data = data1
        notifyDataSetChanged()
    }

    override fun convert(holder: BaseViewHolder, item: AuthResponseBody) {
        holder.setText(R.id.tv_card_name, item.keyName)
        holder.setText(
            R.id.tv_card_number,
            context.getString(R.string.item_card_name_pre, item.keyInfo)
        )
        if (item.devNames.size > 0) {
            var devNameStr = ""
            for ((index, devName) in item.devNames.withIndex()) {
//            for (devName  in item.devNames ) {
                NLog.e("<jake> .. ......区域:: : $devName")
                val name =
                    if (devName.length > 16) {
                        devName.substring(0, 15).plus("...")
                    } else {
                        devName
                    }
                devNameStr += if (index == item.devNames!!.size - 1) {
                    name
                } else {
                    "$name/"
                }

//                devName.let {
//                    devNameStr += "$it "
//                }
            }
            NLog.e("<jake> .. ......devNameStr: $devNameStr")
            holder.setText(
                R.id.tv_card_area,
                context.getString(R.string.item_card_area_pre, devNameStr)
            )
            holder.setTextColor(R.id.tv_card_area, context.resources.getColor(R.color.grey_color))
        } else {
            holder.setText(R.id.tv_card_area, context.getString(R.string.item_card_area_none))
            holder.setTextColor(R.id.tv_card_area, context.resources.getColor(R.color.default_red))
        }

        holder.getView<ImageView>(R.id.iv_card_delete).setOnClickListener {
            mOnDeleteClickListener?.onDeleteClick(item)
        }

        holder.getView<View>(R.id.cl).setOnClickListener {
            mOnItemClickListener?.onClick(item)
        }
    }

    interface OnDeleteClickListener {
        fun onDeleteClick(item: AuthResponseBody)
    }

    interface OnItemClickListener {
        fun onClick(item: AuthResponseBody)
    }
}